﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using Soomla;
namespace Soomla.Store.Example {
	public class menuMort : MonoBehaviour {

		public Transform panneau_niveau_debloque2;
		public Transform panneau_niveau_debloque3;
		public Transform panneau_niveau_debloque4;
		public Transform panneau_niveau_debloque5;
		public Transform panneau_niveau_debloque6;
		public Transform panneau_niveau_debloque7;
		private Transform panneau_instantie;
		private int goldEarn;
		private int score;
		private bool afficher_panneau;
		private double lastInterval ;
		private double temps_ecoule ;
		private bool continuer;

		int howMuchGold(int s)
		{
			if (s > 210) {
								return ((s - 200) / 10) * 20 + howMuchGold (200);
						} else if (s > 110) {
								return ((s - 100) / 10) * 10 + howMuchGold (100);
						} else if (s > 60) {
								return ((s - 50) / 10) * 5;
						} else {
								return 0;
						}
		}

		// Use this for initialization
		void Start () {
			score = Score.score;
			//gain des golds
			goldEarn = howMuchGold (score);
			continuer = true;
			StoreInventory.GiveItem ("currency_coins", goldEarn);
			//Game center Android, ne fonctionne pas.
			Social.ReportScore(score, "CgkIs8_I_fYfEAIQAQ", (bool success) => {
			});
			afficher_panneau = niveauDebloque ();
			if (afficher_panneau) {
				lastInterval = Time.realtimeSinceStartup;
				panneau_instantie.parent=this.transform;
				panneau_instantie.localScale=new Vector3(1,1,1);
				panneau_instantie.localPosition=new Vector3(0f,0f,-0.1f);
				}
		}

		void Update()
		{
			if(afficher_panneau && continuer)
			{
				temps_ecoule+=Time.realtimeSinceStartup-lastInterval;
				lastInterval = Time.realtimeSinceStartup;
				if(temps_ecoule>=2 )
				{
					GameObject.Destroy(panneau_instantie.gameObject);
					continuer=false;
				}
			}
		}

		//Debloquer les niveaux
		bool niveauDebloque()
		{
			int score_cumule = PlayerPrefs.GetInt ("scoreCumule");
			int best_score= PlayerPrefs.GetInt ("Hight Score");

			if (PlayerPrefs.GetInt ("niveau2Bloque") == 0 && score_cumule >= 2000) {
				print ("ok");
				PlayerPrefs.SetInt("niveau2Bloque",1);
				panneau_instantie=(Transform)Transform.Instantiate(panneau_niveau_debloque2);
				return true;
						}
			else if (PlayerPrefs.GetInt ("niveau3Bloque") == 0 && score_cumule >= 4500) {
				PlayerPrefs.SetInt("niveau3Bloque",1);
				panneau_instantie=(Transform)Transform.Instantiate(panneau_niveau_debloque3);
				return true;

			}
			else if (PlayerPrefs.GetInt ("niveau4Bloque") == 0 && score_cumule >= 6500 && best_score>=150) {
				PlayerPrefs.SetInt("niveau4Bloque",1);
				panneau_instantie=(Transform)Transform.Instantiate(panneau_niveau_debloque4);
				return true;

			}
			else if (PlayerPrefs.GetInt ("niveau5Bloque") == 0 && score_cumule >= 10000 && best_score>=175) {
				PlayerPrefs.SetInt("niveau5Bloque",1);
				panneau_instantie=(Transform)Transform.Instantiate(panneau_niveau_debloque5);
				return true;

			}
			else if (PlayerPrefs.GetInt ("nivea6Bloque") == 0 && score_cumule >= 15000 && best_score>=200) {
				PlayerPrefs.SetInt("niveau6Bloque",1);
				panneau_instantie=(Transform)Transform.Instantiate(panneau_niveau_debloque6);
				return true;

			}
			else if (PlayerPrefs.GetInt ("niveau7Bloque") == 0 && score_cumule >= 20000 && best_score>=250) {
				PlayerPrefs.SetInt("niveau7Bloque",1);
				panneau_instantie=(Transform)Transform.Instantiate(panneau_niveau_debloque7);
				return true;

			}
			return false;
		}



	}
}
